

package com.viweb.storemanager.core;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.table.TableCellRenderer;

public class HeaderRenderer extends JLabel implements TableCellRenderer {
    public static final int NONE = 0;
    public static final int DOWN = 1;
    public static final int UP = 2;
    
    private boolean  _pushedColumn = false;
    private int _sortstyle = 0;
    
    
    public void setPushedColumn(boolean pushedColumn) {
        _pushedColumn = pushedColumn;
    }
    
    public void setSortStyle(int sortstyle) {
        _sortstyle = sortstyle;
    }
    
    public int getSortStyle() {
        return _sortstyle;
    }
    
    public HeaderRenderer(int alignment) {
        setSize(getSize().width, 50);
        setHorizontalAlignment(alignment);
        setFont(new Font("Tahoma", Font.PLAIN, 11));
        setHorizontalTextPosition(LEFT);
    }
    
    public Component getTableCellRendererComponent(JTable table,
            Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        
        table.getTableHeader().setBackground(new Color(236, 233, 216));
        
        setText((value == null) ? "" :
            getHorizontalAlignment() == SwingConstants.LEFT ? " " + value.toString().trim() :
                getHorizontalAlignment() == SwingConstants.RIGHT ? value.toString().trim() + " " : value.toString().trim());
        setBorder(UIManager.getBorder("TableHeader.cellBorder"));
        setIcon(null);
        
        if (_pushedColumn) {
            if (_sortstyle != NONE) {
                if (_sortstyle == DOWN) {
                    setIcon(new BevelArrowIcon(BevelArrowIcon.DOWN, false, false));
                } else {
                    setIcon(new BevelArrowIcon(BevelArrowIcon.UP, false, false));
                }
            }
        }
        
        return this;
    }
    
    public void refresh() {
        try {
            setText(getText());
            setBorder(BorderFactory.createEtchedBorder(1));
            setIcon(null);
            
            if (_pushedColumn) {
                if (_sortstyle != NONE) {
                    if (_sortstyle == DOWN) {
                        setIcon(new BevelArrowIcon(BevelArrowIcon.DOWN, false, false));
                    } else {
                        setIcon(new BevelArrowIcon(BevelArrowIcon.UP, false, false));
                    }
                }
            }
        } catch (Exception ex) {
        }
    }
    
    class BevelArrowIcon implements Icon {
        public static final int UP = 0; // direction
        public static final int DOWN = 1;
        private static final int DEFAULT_SIZE = 11;
        
        private Color edge1;
        private Color edge2;
        private Color fill;
        private int size;
        private int direction;
        
        public BevelArrowIcon(int direction, boolean isRaisedView, boolean isPressedView) {
            if (isRaisedView) {
                if (isPressedView) {
                    init(UIManager.getColor("controlLtHighlight"), UIManager
                            .getColor("controlDkShadow"), UIManager
                            .getColor("controlShadow"), DEFAULT_SIZE, direction);
                } else {
                    init(UIManager.getColor("controlHighlight"), UIManager
                            .getColor("controlShadow"), UIManager
                            .getColor("control"), DEFAULT_SIZE, direction);
                }
            } else {
                if (isPressedView) {
                    init(UIManager.getColor("controlDkShadow"), UIManager
                            .getColor("controlLtHighlight"), UIManager
                            .getColor("controlShadow"), DEFAULT_SIZE, direction);
                } else {
                    init(UIManager.getColor("controlShadow"), UIManager
                            .getColor("controlHighlight"), UIManager
                            .getColor("control"), DEFAULT_SIZE, direction);
                }
            }
        }
        
        public BevelArrowIcon(Color edge1, Color edge2, Color fill, int size, int direction) {
            init(edge1, edge2, fill, size, direction);
        }
        
        public void paintIcon(Component c, Graphics g, int x, int y) {
            switch (direction) {
                case DOWN:
                    drawDownArrow(g, x, y);
                    break;
                case UP:
                    drawUpArrow(g, x, y);
                    break;
            }
        }
        
        public int getIconWidth() {
            return size;
        }
        
        public int getIconHeight() {
            return size;
        }
        
        private void init(Color edge1, Color edge2, Color fill, int size, int direction) {
            this.edge1 = edge1;
            this.edge2 = edge2;
            this.fill = fill;
            this.size = size;
            this.direction = direction;
        }
        
        private void drawDownArrow(Graphics g, int xo, int yo) {
            g.setColor(edge1);
            g.drawLine(xo, yo, xo + size - 1, yo);
            g.drawLine(xo, yo + 1, xo + size - 3, yo + 1);
            g.setColor(edge2);
            g.drawLine(xo + size - 2, yo + 1, xo + size - 1, yo + 1);
            int x = xo + 1;
            int y = yo + 2;
            int dx = size - 6;
            while (y + 1 < yo + size) {
                g.setColor(edge1);
                g.drawLine(x, y, x + 1, y);
                g.drawLine(x, y + 1, x + 1, y + 1);
                if (0 < dx) {
                    g.setColor(fill);
                    g.drawLine(x + 2, y, x + 1 + dx, y);
                    g.drawLine(x + 2, y + 1, x + 1 + dx, y + 1);
                }
                g.setColor(edge2);
                g.drawLine(x + dx + 2, y, x + dx + 3, y);
                g.drawLine(x + dx + 2, y + 1, x + dx + 3, y + 1);
                x += 1;
                y += 2;
                dx -= 2;
            }
            g.setColor(edge1);
            g.drawLine(xo + (size / 2), yo + size - 1, xo + (size / 2), yo + size
                    - 1);
        }
        
        private void drawUpArrow(Graphics g, int xo, int yo) {
            g.setColor(edge1);
            int x = xo + (size / 2);
            g.drawLine(x, yo, x, yo);
            x--;
            int y = yo + 1;
            int dx = 0;
            while (y + 3 < yo + size) {
                g.setColor(edge1);
                g.drawLine(x, y, x + 1, y);
                g.drawLine(x, y + 1, x + 1, y + 1);
                if (0 < dx) {
                    g.setColor(fill);
                    g.drawLine(x + 2, y, x + 1 + dx, y);
                    g.drawLine(x + 2, y + 1, x + 1 + dx, y + 1);
                }
                g.setColor(edge2);
                g.drawLine(x + dx + 2, y, x + dx + 3, y);
                g.drawLine(x + dx + 2, y + 1, x + dx + 3, y + 1);
                x -= 1;
                y += 2;
                dx += 2;
            }
            g.setColor(edge1);
            g.drawLine(xo, yo + size - 3, xo + 1, yo + size - 3);
            g.setColor(edge2);
            g.drawLine(xo + 2, yo + size - 2, xo + size - 1, yo + size - 2);
            g.drawLine(xo, yo + size - 1, xo + size, yo + size - 1);
        }
        
    }
    
    class BlankIcon implements Icon {
        private Color fillColor;
        
        private int size;
        
        public BlankIcon() {
            this(null, 11);
        }
        
        public BlankIcon(Color color, int size) {
            fillColor = color;
            
            this.size = size;
        }
        
        public void paintIcon(Component c, Graphics g, int x, int y) {
            if (fillColor != null) {
                g.setColor(fillColor);
                g.drawRect(x, y, size - 1, size - 1);
            }
        }
        
        public int getIconWidth() {
            return size;
        }
        
        public int getIconHeight() {
            return size;
        }
    }
}
